package edu.zhku.ttms.view.dialog;

import edu.zhku.ttms.domain.Car;
import edu.zhku.ttms.service.CarService;
import edu.zhku.ttms.service.impl.CarServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;
import edu.zhku.ttms.view.panel.content.CarManagementPanel;

import javax.swing.*;
import java.awt.*;

/**
 * @author 泽
 * @since 2021/4/26 10:31
 */
public class CarModDialog extends JDialog {

    static CarService carService = new CarServiceImpl();

    public CarModDialog(Car car) {
        super(ViewController.getFrame(), "修改车辆", true);
        this.setLayout(null);
        this.setBounds(560, 395, 440, 200);

        Font font = new LeftLabelFont();

        int x = 10;
        int y = 0;
        int fieldWidth = 300;
        int labelWidth = 100;
        int buttonWidth = 100;
        int height = 25;
        int margin = 5;

        int incrementY = height + margin;

        JLabel licensePlateNumberLabel = new JLabel("车牌号");
        licensePlateNumberLabel.setFont(font);
        licensePlateNumberLabel.setBounds(x, y, labelWidth, height);
        this.add(licensePlateNumberLabel);

        JTextField licensePlateNumberInput = new JTextField();
//        licensePlateNumberInput.setFont(font);
        licensePlateNumberInput.setText(car.getLicensePlateNumber());
        licensePlateNumberInput.setBounds(x + labelWidth, y, fieldWidth, height);
        this.add(licensePlateNumberInput);

        JLabel carDrivingLicenseNumberLabel = new JLabel("行驶证号");
        carDrivingLicenseNumberLabel.setFont(font);
        carDrivingLicenseNumberLabel.setBounds(x, y + incrementY, labelWidth, height);
        this.add(carDrivingLicenseNumberLabel);

        JTextField carDrivingLicenseNumberInput = new JTextField();
        carDrivingLicenseNumberInput.setText(car.getCarDrivingLicenseNumber());
        carDrivingLicenseNumberInput.setBounds(x + labelWidth, y + incrementY, fieldWidth, height);
        this.add(carDrivingLicenseNumberInput);

        JLabel ownerNameLabel = new JLabel("车主姓名");
        ownerNameLabel.setFont(font);
        ownerNameLabel.setBounds(x, y + 2 * incrementY, labelWidth, height);
        this.add(ownerNameLabel);

        JTextField ownerNameInput = new JTextField();
        ownerNameInput.setText(car.getOwnerName());
        ownerNameInput.setBounds(x + labelWidth, y + 2 * incrementY, fieldWidth, height);
        this.add(ownerNameInput);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(x + labelWidth, y + 3 * incrementY, fieldWidth, height);
        this.add(tip);

        JButton carAddButton = new JButton("修改");
        carAddButton.addActionListener(e -> {
            String licensePlateNumber = licensePlateNumberInput.getText();
            String carDrivingLicenseNumber = carDrivingLicenseNumberInput.getText();
            String ownerName = ownerNameInput.getText();
            String res = carService.modify(new Car(car.getCarId(), licensePlateNumber, carDrivingLicenseNumber, ownerName));
            if (CheckUtil.isSuccess(res)) {
                this.setVisible(false);
                ViewController.changeView(new CarManagementPanel());
            } else {
                tip.setText(res);
            }
        });
        carAddButton.setBounds(x + labelWidth, y + 4 * incrementY, buttonWidth, height);
        this.add(carAddButton);
    }

}
